﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.SessionState;

namespace lcsign.Web.documentinfo
{
    /// <summary>
    /// GetDoc 的摘要说明
    /// </summary>
    public class GetDoc : IHttpHandler, IRequiresSessionState
    {
        lcsign.BLL.documentinfo bll = new lcsign.BLL.documentinfo();

        public void ProcessRequest(HttpContext context)
        {

            string ac = "";
            if (null != context.Request.Form["action"])
            {//获取前台传来的值  
                ac = context.Request.Form["action"].ToString().Trim();
            }
            else if (null != context.Request.QueryString["action"])
            {
                ac = context.Request.QueryString["action"].ToString().Trim();
            }

            if (ac == "query")
                Query(context);
            else if (ac == "delete")
                delete(context);           
            else if (ac == "add")
                add(context);
            else if (ac == "modify")
                modify(context);
            else if (ac == "querymy")
                Querymy(context);
            else if (ac == "bindDDL")
                bindDDL(context);  
        }

        public void bindDDL(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            
            DataSet ds = new Maticsoft.BLL.documentFlowMapping().GetDistinctList("");

            string strJson = ToJson.DataTable2JsonNormal(ds.Tables[0]);//DataSet数据转化为Json数据                
            context.Response.Write(strJson);//返回给前台页面  
            context.Response.End();
        }

        public void add(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            int uid = Web.Admin.myinfo.getID(context);
            if (uid > 0)
            {
                string title = context.Request.Form["dtitle"];
                string did = context.Request.QueryString["did"];
                
                lcsign.Model.documentinfo model = new lcsign.Model.documentinfo();
                model.dtitle = title;
                model.ddatetime = DateTime.Now;
                model.uid = uid;
                model.dstatus = 0;
                model.upfile= did;
                int rid = bll.Add(model);

                Maticsoft.BLL.documentFlowMapping dmb = new Maticsoft.BLL.documentFlowMapping();

                string documentFlowMapping_name = context.Request.Form["cc"];//下拉列表框的值

                DataSet ds_dm = dmb.GetList(" documentFlowMapping_name='" + documentFlowMapping_name + "' order by flow_order  ");

                Maticsoft.BLL.documentFlowInfo dfb = new Maticsoft.BLL.documentFlowInfo();
                Maticsoft.Model.documentFlowInfo df = new Maticsoft.Model.documentFlowInfo();

                if (ds_dm.Tables[0].Rows.Count > 0)
                {
                    df.flow_id = Convert.ToInt32(ds_dm.Tables[0].Rows[0]["flow_id"].ToString());
                    df.flow_name = ds_dm.Tables[0].Rows[0]["flow_name"].ToString();
                    df.document_id = rid;

                    DataSet ds_flowNode = new Maticsoft.BLL.flow_node().GetList(" flow_id=" + df.flow_id + " and flow_node_num = 1");

                    df.flow_node_num_now = 1;

                    if (ds_flowNode.Tables[0].Rows.Count == 1)
                    {
                        df.flow_node_id_now = Convert.ToInt32(ds_flowNode.Tables[0].Rows[0]["flow_node_id"]);
                    }
                    df.isDone = false;
                    df.status = 0;

                    foreach (DataRow row in ds_dm.Tables[0].Rows)
                    {
                        df.flow_ids += "|" + row["flow_id"].ToString();//将此公文的所有经过流程记录
                    }
                    df.documentFlowMapping_name = documentFlowMapping_name;

                    dfb.Add(df);
                }

                if (rid > 0)
                {
                    context.Response.Write("T");//返回给前台页面  
                    context.Response.End();
                }
                else
                {
                    context.Response.Write("F");//返回给前台页面  
                    context.Response.End();
                }
            }
            else
            {
                context.Response.Write("L");//返回给前台页面  
                context.Response.End();
            }
        }
        public void modify(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            int uid = Web.Admin.myinfo.getID(context);
            if (uid > 0)
            {
                int did = 0;
                string title = context.Request.Form["dtitle"];
                string uidstr = context.Request.Form["did"];
                if (uidstr != "")
                    did = int.Parse(uidstr);
                lcsign.Model.documentinfo model = new lcsign.Model.documentinfo();
                model.dtitle = title;
                model.did = did;
                if (bll.Update(model))
                {
                    context.Response.Write("T");//返回给前台页面  
                    context.Response.End();
                }
                else
                {
                    context.Response.Write("F");//返回给前台页面  
                    context.Response.End();
                }
            }
            else
            {
                context.Response.Write("L");//返回给前台页面  
                context.Response.End();
            }
        }
        public void delete(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            if (Web.Admin.myinfo.isLogin(context))
            {
                int uid = 0;
                int myid = Web.Admin.myinfo.getID(context);
                string uidstr = context.Request.QueryString["did"];
                if (uidstr != "")
                    uid = int.Parse(uidstr);
                string path = HttpContext.Current.Server.MapPath("/");
                if (bll.Delete(uid, myid,path))
                {
                    context.Response.Write("T");//返回给前台页面  
                    context.Response.End();
                }
                else
                {
                    context.Response.Write("F");//返回给前台页面  
                    context.Response.End();
                }
            }
            else
            {
                context.Response.Write("F");//返回给前台页面  
                context.Response.End();
            }
        }
        public void Query(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            int uid = Web.Admin.myinfo.getID(context);
            if (uid > 0)
            {
                //===============================================================  
                //获取查询条件:【用户id,开始时间，结束时间，关键字】  
                string uname, utruename, st;
                uname = utruename = st = "";
                //获取前台传来的值 
                if (null != context.Request.Form["search_type"])
                {//获取前台传来的值  
                    st = context.Request.Form["search_type"].ToString().Trim();
                }
                if (null != context.Request.Form["search_value"])
                {//获取前台传来的值  
                    uname = context.Request.Form["search_value"].ToString().Trim();
                }
                if (st == "username")
                {
                    utruename = uname;
                    uname = "";
                }
                //================================================================  
                //获取分页和排序信息：页大小，页码，排序方式，排序字段  
                int pageRows, page;
                pageRows = 10;
                page = 1;
                string order, sort, oderby; order = sort = oderby = "";
                if (null != context.Request.Form["rows"])
                {
                    pageRows = int.Parse(context.Request.Form["rows"].ToString().Trim());
                }
                if (null != context.Request.Form["page"])
                {
                    page = int.Parse(context.Request.Form["page"].ToString().Trim());
                }
                if (null != context.Request.Form["sort"])
                {
                    order = context.Request.Form["sort"].ToString().Trim();
                }
                if (null != context.Request.Form["order"])
                {
                    sort = context.Request.Form["order"].ToString().Trim();
                }
                //===================================================================  
                //组合查询语句：条件+排序  
                StringBuilder strWhere = new StringBuilder();
                if (uname != "")
                {
                    strWhere.AppendFormat(" title like '%{0}%' and ", uname);
                }
                strWhere.AppendFormat(" uid={0} and ", uid);

                //删除多余的and  
                int startindex = strWhere.ToString().LastIndexOf("and");//获取最后一个and的位置  
                if (startindex >= 0)
                {
                    strWhere.Remove(startindex, 3);//删除多余的and关键字  
                }
                if (sort != "" && order != "")
                {
                    //strWhere.AppendFormat(" order by {0} {1}", sort, order);//添加排序  
                    oderby = order + " " + sort;
                }
                //DataSet ds = Bnotice.GetList(strWhere.ToString());  //调用不分页的getlist  

                //调用分页的GetList方法  
                DataSet ds = bll.GetListByPage(strWhere.ToString(), oderby, (page - 1) * pageRows + 1, page * pageRows);
                int count = bll.GetRecordCount(strWhere.ToString());//获取条数  
                string strJson = ToJson.Dataset2Json(ds, count, 50);//DataSet数据转化为Json数据                
                context.Response.Write(strJson);//返回给前台页面  
                context.Response.End();
            }

        }
        
        public void Querymy(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            int uid = Web.Admin.myinfo.getID(context);
            if (uid > 0)
            {
                string uname = "", StartTime = "", EndTime = "", title = "";
                //获取前台传来的值 
                string st= "";
                //获取前台传来的值 
                if (null != context.Request.Form["stitle"])
                {//获取前台传来的值  
                    st = context.Request.Form["stitle"].ToString().Trim();
                }                
               
                if (null != context.Request.Form["StartTime"])
                {//获取前台传来的值  
                    StartTime = context.Request.Form["StartTime"].ToString().Trim();
                }
                if (null != context.Request.Form["EndTime"])
                {//获取前台传来的值  
                    EndTime = context.Request.Form["EndTime"].ToString().Trim();
                }
                
                //================================================================  
                //获取分页和排序信息：页大小，页码，排序方式，排序字段  
                int pageRows, page;
                pageRows = 10;
                page = 1;
                string order, sort, oderby; order = sort = oderby = "";
                if (null != context.Request.Form["rows"])
                {
                    pageRows = int.Parse(context.Request.Form["rows"].ToString().Trim());
                }
                if (null != context.Request.Form["page"])
                {
                    page = int.Parse(context.Request.Form["page"].ToString().Trim());
                }
                if (null != context.Request.Form["sort"])
                {
                    order = context.Request.Form["sort"].ToString().Trim();
                }
                if (null != context.Request.Form["order"])
                {
                    sort = context.Request.Form["order"].ToString().Trim();
                }
                //===================================================================  
                //组合查询语句：条件+排序  
                StringBuilder strWhere = new StringBuilder();
                if (st != "")
                {
                    strWhere.AppendFormat(" dtitle like '%{0}%' and ", uname);
                }
                if (StartTime != "")
                {
                    strWhere.AppendFormat(" infotime>='{0}' and ", StartTime);
                }
                if (EndTime != "")
                {
                    strWhere.AppendFormat(" infotime<='{0}' and ", EndTime);
                }
                
                strWhere.AppendFormat(" uid={0} and ", uid);

                //删除多余的and  
                int startindex = strWhere.ToString().LastIndexOf("and");//获取最后一个and的位置  
                if (startindex >= 0)
                {
                    strWhere.Remove(startindex, 3);//删除多余的and关键字  
                }
                if (sort != "" && order != "")
                {
                    //strWhere.AppendFormat(" order by {0} {1}", sort, order);//添加排序  
                    oderby = order + " " + sort;
                }
                //DataSet ds = Bnotice.GetList(strWhere.ToString());  //调用不分页的getlist  

                //调用分页的GetList方法  
                DataSet ds = bll.GetListByPage(strWhere.ToString(), oderby, (page - 1) * pageRows + 1, page * pageRows);
                int count = bll.GetRecordCount(strWhere.ToString());//获取条数  
                string strJson = ToJson.Dataset2Json(ds, count, 50);//DataSet数据转化为Json数据                
                context.Response.Write(strJson);//返回给前台页面  
                context.Response.End();
            }

        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}